package com.yyk.yiliao.ui.trade;

import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankUserdetailedInfo;
import com.yyk.yiliao.widget.EndLessOnScrollListener;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 明细  记录
 */
public class MingxiActivity extends BaseActivity {

    @BindView(R.id.rv_mingxi)
    RecyclerView rvMingxi;
    private List<UserbankUserdetailedInfo.DataBean> mxList = new ArrayList<>();
    private BaseRecyclerAdapter<UserbankUserdetailedInfo.DataBean> mxAdapter;
    private int currPage = 1;


    @Override
    public int getLayoutId() {
        return R.layout.activity_mingxi;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("明细");
    }

    @Override
    public void initData() {
        initAdapter();
        iniitPost(true, currPage);

    }

    private void iniitPost(boolean flag, int page) {
        Map<String, String> map = new TreeMap<>();

        map.put("id", Hawk.get("uid") + "");
        map.put("page", page + "");
        map.put("num", 10 + "");
        map.put("sign", Md5Util2.createSign(map));

        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankUserdetailed(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<UserbankUserdetailedInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        Logger.e("明细列表" + "异常");

                    }

                    @Override
                    public void onNext(UserbankUserdetailedInfo info) {
                        Logger.e("明细列表" + info.toString());
                        if (info.getCode() == 1) {
                            if (flag) {
                                mxList.clear();
                            }
                            if (info.getData() != null && info.getData().size() > 0) {
                                mxList.addAll(info.getData());
                            }
                            mxAdapter.notifyDataSetChanged();
                        }

                    }
                });
    }

    /**
     * 1.明细列表
     */
    private void initAdapter() {
        //1.明细列表

        mxAdapter = new BaseRecyclerAdapter<UserbankUserdetailedInfo.DataBean>(mActivity, mxList, R.layout.adapter_item_mingxi) {
            @Override
            public void convert(BaseRecyclerHolder holder, UserbankUserdetailedInfo.DataBean item, int position, boolean isScrolling) {
                holder.setText(R.id.tv_busitype, item.getBusitype());
                holder.setText(R.id.tv_addtime, item.getAddtime());
                holder.setText(R.id.tv_statemoney, item.getState() + item.getMoney());
            }
        };
        rvMingxi.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_item_e6_05, 0));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        rvMingxi.setLayoutManager(linearLayoutManager);
        rvMingxi.setAdapter(mxAdapter);

        mxAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("order_code", mxList.get(position).getOrder_code());
                bundle.putString("type", mxList.get(position).getBusitype());
                startAct(Mingxi2Activity.class, bundle);
            }
        });
        //TODO  瑕疵    这里需要禁止误操作请求，判断第一次进入界面 1=1,不请求下一步操作
        rvMingxi.addOnScrollListener(new EndLessOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                if (currPage == currentPage) {
                    return;
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        iniitPost(false, currentPage);
                        Logger.e("currPage:" + currPage);
                        Logger.e("currentPage:" + currentPage);
                    }
                }, 1000);


            }
        });
    }
}
